package org.xqyy.eval.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xqyy.eval.excelmodel.DutyDetailInfo;
import org.xqyy.eval.excelmodel.EvalDutyInfo;
import org.xqyy.eval.excelmodel.RankBean;
import org.xqyy.eval.model.DutyFile;
import org.xqyy.eval.service.IReportService;
import org.xqyy.eval.util.DataFormatUtil;
import org.xqyy.eval.util.EasyPoiUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private IReportService reportService;

    /**
     * 排名页面跳转
     * @return
     */
    @RequestMapping("rankpage")
    public ModelAndView rankpage(){
        ModelAndView modelAndView = new ModelAndView();
        Date date = Calendar.getInstance().getTime();
        String reportDate =  DataFormatUtil.defectDateFormat.format(date);
        modelAndView.addObject("reportDate",reportDate);
        modelAndView.setViewName("jsp/report/rank");
        return  modelAndView;
    }

    /**
     * 排名数据查询
     * @param cycledate
     * @param dutytype
     * @return
     */
    @RequestMapping("rankdata")
    @ResponseBody
    public List<RankBean> rankdata(String cycledate,String dutytype){
        List<RankBean> rankBeans = reportService.selectRank(cycledate,dutytype);
        return  rankBeans;
    }

    /**
     * 排名Excel下载
     * @param cycledate
     * @param dutytype
     * @param response
     */
    @RequestMapping("rankExcel")
    public  void  downExcel(String cycledate,String dutytype,HttpServletResponse response){
        List<RankBean> rankBeans =  reportService.selectRank(cycledate,dutytype);
        String month = "";
        try {
            Date date = DataFormatUtil.defectDateFormat.parse(cycledate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            month = calendar.get(Calendar.MONTH)+1+"月";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        EasyPoiUtil.exportExcel(rankBeans,dutytype+month+"满意度排名",dutytype,
                RankBean.class,dutytype+".xls",true,response);

    }


    /**
     * 科室填报情况页面跳转
     * @return
     */
    @RequestMapping("dutyfilereportpage")
    public ModelAndView dutyfilereportpage(){
        ModelAndView modelAndView = new ModelAndView();
        Date date = Calendar.getInstance().getTime();
        String reportDate =  DataFormatUtil.defectDateFormat.format(date);
        modelAndView.addObject("reportDate",reportDate);
        modelAndView.setViewName("jsp/report/dutyfilereport");
        return  modelAndView;
    }

    /**
     * 查询科室填报情况
     * @param cycledate
     * @return
     */
    @RequestMapping("dutyfilereport")
    @ResponseBody
    public List<DutyFile> dutyfilereport(String cycledate){
        List<DutyFile> dutyFiles = reportService.selectDutyFile(cycledate);
        return  dutyFiles;
    }

    /**
     * 科室填报情况Excel下载
     * @param cycledate
     * @param response
     */
    @RequestMapping("dutyfileExcel")
    public  void  dutyfileExcel(String cycledate,HttpServletResponse response){
        List<DutyFile> dutyFiles =  reportService.selectDutyFile(cycledate);
        String month = "";
        try {
            Date date = DataFormatUtil.defectDateFormat.parse(cycledate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            month = calendar.get(Calendar.MONTH)+1+"月";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        EasyPoiUtil.exportExcel(dutyFiles,month+"科室填报情况","科室填报情况",
                DutyFile.class,"科室填报情况.xls",true,response);

    }

    /**
     * 科室评价明细情况页面跳转
     * @return
     */
    @RequestMapping("dutyEvalDetaillPage")
    public ModelAndView dutyEvalDetaillPage(){
        ModelAndView modelAndView = new ModelAndView();
        Date date = Calendar.getInstance().getTime();
        String reportDate =  DataFormatUtil.defectDateFormat.format(date);
        modelAndView.addObject("reportDate",reportDate);
        modelAndView.setViewName("jsp/report/dutyevaldetaill");
        return  modelAndView;
    }

    /**
     * 科室评价明细情况Excel下载
     * @param cycledate
     * @param response
     */
    @RequestMapping("dutyEvalDetaillExcel")
    public  void  dutyEvalDetaillExcel(String cycledate,String dutytype,HttpServletResponse response){
        List<EvalDutyInfo> evalDutyInfos =  reportService.selectDutyEvalDetail(cycledate,dutytype);
        List<ExcelExportEntity> colList =  reportService.dutyEvalDetailTableHead(dutytype);
        List<Map<String, Object>> list = reportService.dutyEvalDetailTableData(dutytype,evalDutyInfos);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(dutytype+"科室评价情况", dutytype+"科室评价情况"), colList,
                list);
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(dutytype+"科室评价情况.xls", "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new  RuntimeException(e);
        }
    }

}
